package game.actions;

import edu.monash.fit2099.engine.actions.Action;
import edu.monash.fit2099.engine.actors.Actor;
import edu.monash.fit2099.engine.positions.GameMap;
import edu.monash.fit2099.engine.weapons.Weapon;

/**
 * Generic attack action:
 * - If a specific Weapon is provided, use it;
 * - otherwise, use the actor's IntrinsicWeapon (e.g. fist, bite, claw).
 */
public class AttackAction extends Action {

    private final Actor target;
    private final String direction;
    private final Weapon weapon; // may be null -> use intrinsic

    /** Use attacker's intrinsic weapon. */
    public AttackAction(Actor target, String direction) {
        this(target, direction, null);
    }

    /** Use a specific weapon (e.g., weapon item or custom weapon). */
    public AttackAction(Actor target, String direction, Weapon weapon) {
        this.target = target;
        this.direction = direction;
        this.weapon = weapon;
    }

    @Override
    public String execute(Actor actor, GameMap map) {
        Weapon w = this.weapon;
        if (w == null) {
            // fall back to intrinsic weapon (engine guarantees Actor has one)
            w = actor.getIntrinsicWeapon();
        }

        String result = w.attack(actor, target, map);

        // If target collapses, delegate to engine's unconscious hook
        if (!target.isConscious()) {
            result += "\n" + target.unconscious(actor, map);
        }
        return result;
    }

    @Override
    public String menuDescription(Actor actor) {
        return actor + " attacks " + target + " at " + direction + " with " + (weapon != null ? weapon : "Intrinsic Weapon");
    }
}
